package cn.icanci.ladybird.controller;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.pojo.bo.AdminUpdateTicketBO;
import cn.icanci.ladybird.pojo.bo.AdminUploadTicketBO;
import cn.icanci.ladybird.pojo.dto.UserProduceDTO;
import cn.icanci.ladybird.pojo.entity.TicketDetails;
import cn.icanci.ladybird.service.TicketDetailsService;
import cn.icanci.ladybird.service.admin.AdminUpdateTicketService;
import cn.icanci.ladybird.service.admin.AdminUploadTicketService;
import cn.icanci.ladybird.service.user.UserProduceService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @author: icanci
 * @date: Created in 2020/10/7 0:08
 * @classAction: 管理员管理票
 */
@Controller
@Slf4j
@RequestMapping("/api/v1/pri/admin")
public class AdminDataForTicketController {

    @Autowired
    private TicketDetailsService ticketDetailsService;
    @Autowired
    private AdminUploadTicketService adminUploadTicketService;
    @Autowired
    private UserProduceService userProduceService;
    @Autowired
    private AdminUpdateTicketService adminUpdateService;
    @Autowired
    private AdminDataController adminDataController;

    /**
     * 跳转到 ticket-list 票列表
     *
     * @param model 数据处理对象
     * @return 返回视图地址
     */
    @RequestMapping("/ticket-list")
    public String adminTicketList(Model model, Integer pageNum, Integer pageSize) {
        // 页面参数，然后在Service进行分页
        PageInfo<TicketDetails> ticketDetailsPageInfo = ticketDetailsService.queryAllByNotObject(pageNum, pageSize);
        JsonData jsonData = JsonData.builderSuccess(ticketDetailsPageInfo);
        model.addAttribute("jsonData", jsonData);
        return "admin/ticket-list";
    }

    /**
     * 跳转到 ticket-add 管理员添加票页面
     *
     * @param model 数据处理对象
     * @return 返回视图地址
     */
    @RequestMapping("/ticket-add")
    public String adminTicketAdd(Model model) {
        return "admin/ticket-add";
    }

    @Deprecated
    @ResponseBody
    @PostMapping("/upload-ticket-demo")
    public JsonData adminUploadTicket(@Param("ticketName") String ticketName,
                                      @Param("ticketStatus") String ticketStatus,
                                      @Param("isKilled") String isKilled,
                                      @Param("city") String city,
                                      @Param("venue") String venue,
                                      @Param("time1") String time1,
                                      @Param("time2") String time2,
                                      @Param("time3") String time3,
                                      @Param("time4") String ticketWeight,
                                      @Param("prices") String prices,
                                      @RequestParam("fileName") List<MultipartFile> files) {
        System.out.println(ticketName);
        System.out.println(ticketStatus);
        System.out.println(isKilled);
        System.out.println(city);
        System.out.println(venue);
        System.out.println(time1);
        System.out.println(time2);
        System.out.println(time3);
        System.out.println(ticketWeight);
        System.out.println(prices);
        for (MultipartFile file : files) {
            System.out.println(file.getSize());
        }
        return JsonData.builderSuccess();
    }

    @PostMapping("/upload-ticket")
    public String adminUploadTicket(Model model, HttpServletRequest request, AdminUploadTicketBO adminUploadTicketBO) throws IOException {
        try {
            JsonData jsonData = adminUploadTicketService.uploadHandler(request, adminUploadTicketBO);
            System.err.println(jsonData);
            // 判断
            if (null == jsonData || jsonData.getCode() == -1) {
                model.addAttribute("jsonData", jsonData);
                return adminDataController.admin500(model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 默认就是首页
        return adminTicketList(model, 1, 6);
    }


    @PostMapping("/update-ticket")
    public String adminUpdateTicket(Model model, HttpServletRequest request, AdminUpdateTicketBO adminUpdateTicketBO) throws IOException {
        try {
            JsonData jsonData = adminUpdateService.updateHandler(request, adminUpdateTicketBO);
            System.err.println(jsonData);
            // 判断
            if (null == jsonData || jsonData.getCode() == -1) {
                model.addAttribute("jsonData", jsonData);
                return adminDataController.admin500(model);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 默认就是首页
        return adminTicketList(model, 1, 6);
    }

    @RequestMapping("/update-ticket-status")
    public String adminUpdateTicketStatus(Model model, Long ticketId, Integer ticketStatus) throws IOException {
        TicketDetails ticketDetails = ticketDetailsService.queryById(ticketId);
        ticketDetails.setTicketDetailsStatus(ticketStatus);
        ticketDetailsService.update(ticketDetails);
        // 判断
        // 默认就是首页
        return adminTicketList(model, 1, 6);
    }


    /**
     * 跳转到 ticket-details
     *
     * @param model 数据处理对象
     * @return 返回视图地址
     */
    @RequestMapping("/ticket-details")
    public String adminTicketDetails(Model model, Long id) {
        if (null == id) {
            JsonData jsonData = JsonData.builderError("数据异常，请重试");
            model.addAttribute("jsonData", jsonData);
            return "admin/500";
        }
        UserProduceDTO userProduceDTO = userProduceService.queryUserProduceDTO(id);
        JsonData jsonData = JsonData.builderSuccess(userProduceDTO);
        model.addAttribute("jsonData", jsonData);
        return "admin/ticket-details";
    }

}
